﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using EHTWebsite.Code;
using EHTWebsiteProject.Code.Entity;

namespace EHTWebsiteProject.Code.BLL
{
    public class DanhMucMenusProvider
    {
        public static Int32 UIDanhMucMenus(DanhMucMenus theDanhMucMenus, bool isAdd)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_IUDanhMucMenus";
            command.CommandType = CommandType.StoredProcedure;

            SqlParameter theDanhMucMenusParameter = new SqlParameter();

            theDanhMucMenusParameter.ParameterName = "@MenuIDOut";
            theDanhMucMenusParameter.Direction = ParameterDirection.Output;
            theDanhMucMenusParameter.DbType = DbType.Int32;

            command.Parameters.Add(theDanhMucMenusParameter);

            command.Parameters.Add(new SqlParameter("@ID", theDanhMucMenus.ID));
            command.Parameters.Add(new SqlParameter("@TitleOrder", theDanhMucMenus.TitleOrder));
            command.Parameters.Add(new SqlParameter("@Description", theDanhMucMenus.Description));
            command.Parameters.Add(new SqlParameter("@TypeMenu", theDanhMucMenus.TypeMenu)); 
            command.Parameters.Add(new SqlParameter("@IsAdd", isAdd));

            DataProvider.ExecuteNoneQuery(command);

            return Int32.Parse(command.Parameters["@MenuIDOut"].Value.ToString());
        }

        public static DataSet GetDanhMucMenus(Int32 TypeMenu)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "[proc_GetDanhMucMenus]";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@TypeMenu", TypeMenu));

            return DataProvider.ExecuteDataSet(command);
        }

        public static DanhMucMenus GetDanhMucMenusByID(Int32 ID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "[proc_GetDanhMucMenusByID]";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@ID", ID));

            DataSet ds = DataProvider.ExecuteDataSet(command);
            DanhMucMenus theDanhMucMenus = null;

            if (ds.Tables[0].Rows.Count > 0)
            {
                theDanhMucMenus = new DanhMucMenus();
                theDanhMucMenus.ID = Int32.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                theDanhMucMenus.TitleOrder = ds.Tables[0].Rows[0]["TitleOrder"].ToString();
                theDanhMucMenus.Description = ds.Tables[0].Rows[0]["Description"].ToString();
                theDanhMucMenus.TypeMenu = Int32.Parse(ds.Tables[0].Rows[0]["TypeMenu"].ToString());
                theDanhMucMenus.NoDeleted = Boolean.Parse(ds.Tables[0].Rows[0]["NoDeleted"].ToString());
            }

            return theDanhMucMenus;
        }

        public static void DeleteDanhMucMenus(Int32 ID)
        {           
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_DeleteDanhMucMenus";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@ID", ID));            

            DataProvider.ExecuteNoneQuery(command);
        }

        public static DataSet GetMenusByUserId(String UserID, Int32 TypeMenu)
        {             
            SqlCommand command = new SqlCommand();
            command.CommandText = "[proc_GetMenus]";
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.Add(new SqlParameter("@UserID", UserID));
            command.Parameters.Add(new SqlParameter("@TypeMenu ", TypeMenu));    

            return DataProvider.ExecuteDataSet(command);
        }


        public static void SaveUserRights(String UserID, Int32 MenuID)
        {            
               
                    SqlCommand command = new SqlCommand();
                    command.CommandText = "proc_SaveUserRights";
                    command.CommandType = CommandType.StoredProcedure;

                    command.Parameters.Add(new SqlParameter("@UserID", UserID));
                    command.Parameters.Add(new SqlParameter("@MenuID", MenuID));

                    DataProvider.ExecuteNoneQuery(command);
        }
    }
}